package org.csu.mypetstore.controller.user;

import org.csu.mypetstore.common.CommonResponse;
import org.csu.mypetstore.domain.BO.ItemBO;
import org.csu.mypetstore.domain.PO.*;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.Impl.AccountServiceImpl;
import org.csu.mypetstore.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CatalogController {
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private HttpServletRequest httpServletRequest;

    @ResponseBody
    @GetMapping("/categories")
    public CommonResponse<List<Category>> getCategoryList(){
        List<Category> categoryList = catalogService.getCategoryList();
        if (categoryList == null)
            return CommonResponse.createForError("没有任何种类！");
        return CommonResponse.createForSuccess(categoryList, "获取种类成功！");
    }

    @ResponseBody
    @GetMapping("/categories/{categoryId}")
    public CommonResponse<Category> getCategory(@PathVariable String categoryId){
        Category category = catalogService.getCategory(categoryId);
        if (category == null)
            return CommonResponse.createForError("没有该种类！");
        return CommonResponse.createForSuccess(category, "获取种类成功！");
    }

    @ResponseBody
    @GetMapping("/categories/{categoryId}/products")
    public CommonResponse<List<Product>> getProductListByCategory(@PathVariable String categoryId){
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        if (productList == null)
            return CommonResponse.createForError("没有产品信息！");
        return CommonResponse.createForSuccess(productList, "获取产品成功！");
    }

    @ResponseBody
    @GetMapping("/products/{productId}")
    public CommonResponse<Product> getProduct(@PathVariable String productId){
        Product product = catalogService.getProduct(productId);
        if (product == null)
            return CommonResponse.createForError("没有该产品！");
        return CommonResponse.createForSuccess(product, "获取产品成功！");
    }

    @ResponseBody
    @GetMapping("/products/{productId}/items")
    public CommonResponse<List<ItemBO>> getItemListByProduct(@PathVariable String productId){
        List<ItemBO> itemBOList = catalogService.getItemListByProduct(productId);
        if (itemBOList == null)
            return CommonResponse.createForError("没有商品信息！");
        return CommonResponse.createForSuccess(itemBOList, "获取商品成功！");
    }

    @ResponseBody
    @GetMapping("/items/{itemId}")
    public CommonResponse<ItemBO> getItem(@PathVariable String itemId){
        ItemBO itemBO = catalogService.getItem(itemId);
        if (itemBO == null)
            return CommonResponse.createForError("没有商品信息！");
        return CommonResponse.createForSuccess(itemBO, "获取商品成功！");
    }

    @ResponseBody
    @GetMapping("/accounts/{username}/myItems")
    public CommonResponse<List<MyItem>> getMyItemList(@PathVariable String username){
        if (!username.equals(TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"), "username")))
            return CommonResponse.createForError("你不能查询他人的商品信息!");
        List<MyItem> myItemList = catalogService.getMyItemList(accountService.getAccountByUsername(username));
        if (myItemList == null)
            return CommonResponse.createForError("没有商品信息！");
        return CommonResponse.createForSuccess(myItemList, "获取商品成功！");
    }
//
//    //进入主页的时候获得所有Category
//    @GetMapping("/main")
//    public String viewMain(Model model){
//        model.addAttribute("categoryList", catalogService.getAllCategory());
//        return "catalog/Main";
//    }
//
//    @GetMapping("/viewCategory")
//    public String viewCategory(String categoryId, Model model) {
//        if (categoryId != null) {
//            Category category = catalogService.getCategory(categoryId);
//            List<Product> productList = catalogService.getProductListByCategory(categoryId);
//            model.addAttribute("category", category);
//            model.addAttribute("productList", productList);
//            return "catalog/Category";
//        }
//        return "catalog/Main";
//    }
//
//    @GetMapping("/viewProduct")
//    public String viewProduct(@RequestParam("productId") String productId, Model model) {
//        if (productId != null) {
//            Product product = catalogService.getProduct(productId);
//            List<Item> itemList = catalogService.getItemListByProduct(productId);
//            model.addAttribute("product", product);
//            model.addAttribute("itemList", itemList);
//            return "catalog/Product";
//        }
//        return "catalog/Category";
//    }
//
//    @GetMapping("/viewItem")
//    public String viewItem(String itemId, Model model){
//        if (itemId != null){
//            Item item = catalogService.getItem(itemId);
//            String productId = item.getProductId();
//            Product product = catalogService.getProduct(productId);
//
//            System.out.println(product.toString() + "CatalogController");
//
//            model.addAttribute("product", product);
//            model.addAttribute("item", item);
//            return "catalog/Item";
//        }
//        return "catalog/Product";
//    }
//
//    @GetMapping("/autoSearchProduct")
//    @ResponseBody
//    public String autoSearchProduct(String term){
//        if(term != null){
//            List<Product> productList = catalogService.searchProductList(term);
//            Iterator<Product> iterator = productList.iterator();
//            ArrayList<String> nameStringList = new ArrayList<>();
//            while(iterator.hasNext())
//                nameStringList.add(iterator.next().getName());
//            Object[] nameStringObjectArray = nameStringList.toArray();//将获取的数据转化为对象数组
//            String[] nameStringArray = new String[nameStringObjectArray.length];//抽出成为一个个字符串
//            System.arraycopy(nameStringObjectArray, 0, nameStringArray, 0, nameStringObjectArray.length);
//            String jsonString = "[";//拼接json数据
//            for (String s : nameStringArray)
//                jsonString += "{\"label\":\""+s+"\"},";
//            jsonString = jsonString.substring(0, jsonString.length() - 1);
//            jsonString += "]";
//            return jsonString;
//        }
//        return "";
//    }
//
//    @PostMapping("/searchProduct")
//    public String searchProduct(String keyword, Model model){
//        if (keyword != null){
//            List<Product> searchProductList = catalogService.searchProductList(keyword);//找到商品列表
//            model.addAttribute("searchProductList", searchProductList);//更新session的商品列表
//            return "catalog/SearchProducts";
//        }
//        return "catalog/Main";
//    }
//
//    @GetMapping("/returnMyItemList")
//    @ResponseBody
//    public CommonResponse<List<MyItem>> returnMyItemList(Model model){
//        AccountVO accountVO = (AccountVO) model.getAttribute("accountVO");
//        return catalogService.getMyItemList(accountVO);
//    }
//
//    @GetMapping("/viewMyItemList")
//    public String viewMyItemList(Model model){
//        return "account/viewMyItemList";
//    }
//
//    @GetMapping("/viewItemInfo/{id}")
//    @ResponseBody
//    public CommonResponse<Product> viewItemInfo(@PathVariable("id") String itemid){
//        Item item = (Item) catalogService.getItem(itemid);
//        return CommonResponse.createForSuccess(item.getProduct());
//    }
}
